package com.unittec.zk.provider.work.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.unittec.zk.provider.utils.StrUtils;
import com.unittec.zk.provider.utils.UUIDUtils;
import com.unittec.zk.provider.work.entity.MesDetail;
import com.unittec.zk.sdk.work.request.Req202003;
import com.unittec.zk.sdk.work.request.Req202008;
import com.unittec.zk.sdk.work.request.Req202009;
import com.unittec.zk.sdk.work.request.Req202010;
import org.springframework.beans.factory.annotation.Autowired;
import com.unittec.zk.sdk.work.request.Req202011;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.unittec.zk.provider.work.entity.MesCollection;
import com.unittec.zk.provider.work.mapper.MesCollectionMapper;

@Service
public class MesCollectionService extends ServiceImpl<MesCollectionMapper, MesCollection> {

    @Resource
    private MesDetailService mesDetailService;

    /**
     * 短信-增/改
     *
     * @param req202008
     * @return
     */
    public Object saveOrUpdateMes(Req202008 req202008) {
        if (req202008.getPhones() != null && req202008.getPhones().size() > 0) {
            //设置短信
            MesCollection mesCollection = new MesCollection();
            if (StrUtil.isBlank(req202008.getId())) {
                mesCollection.setId(UUIDUtils.getId());
            }
            mesCollection.setContent(req202008.getContent());
            mesCollection.setMaxSize(req202008.getContent().length());
            //调用接口发送信息
            String rs = "{\n" +
                    "  \"Message\" : \"OK\",\n" +
                    "  \"RequestId\" : \"F655A8D5-B967-440B-8683-DAD6FF8DE990\",\n" +
                    "  \"Code\" : \"OK\",\n" +
                    "  \"BizId\" : \"900619746936498440^0\"\n" +
                    "}";
            //如果接口返回错误信息，存入errormes
            if (1 == 0) {
                mesCollection.setSendState("2");
                mesCollection.setErrorMsg("从接口中返回的错误描述");
            }
            //如果成功
            else {
                mesCollection.setSendState("1");
            }
            //存信息
            this.saveOrUpdate(mesCollection);
            //存电话
            //先删除原电话
            LambdaQueryWrapper<MesDetail> qw = new LambdaQueryWrapper<>();
            qw.eq(MesDetail::getRelationId, mesCollection.getId());
            mesDetailService.remove(qw);
            //再存新电话
            ArrayList<MesDetail> mesDetails = new ArrayList<>();
            for (String phone : req202008.getPhones()) {
                MesDetail mesDetail = new MesDetail();
                mesDetail.setPhoneNumber(phone);
                mesDetail.setRelationId(mesCollection.getId());
                mesDetails.add(mesDetail);
            }
            mesDetailService.saveBatch(mesDetails);
        }
        return "Success";
    }

    /**
     * 短信分页
     * @param req202009
     * @return
     */
    public Object getMesPage(Req202009 req202009) {
        LambdaQueryWrapper<MesCollection> qw = new LambdaQueryWrapper<>();
        //内容筛选
        if (StrUtil.isNotBlank(req202009.getContent())) {
            qw.apply("content like '%"+req202009.getContent()+"%'");
        }
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        //开始时间筛选
        if (req202009.getSendTimeStart() != null) {
            qw.apply("t1.create_time >'"+df.format(req202009.getSendTimeStart())+"'");
        }
        //结束时间筛选
        if (req202009.getSendTimeEnd() != null) {
            String end = df.format(req202009.getSendTimeEnd());
            qw.apply("(t1.create_time < '"+end+"' or t1.create_time like '"+end+"%')");
        }
        Page<Map<String, Object>> page =this.baseMapper.getPage(new Page<>(req202009.getCurrent(),req202009.getSize()),qw);
        return page;
    }
    /**
     * 發送詳情
     * @param req202010
     * @return
     */

    public Object sendDetail(Req202010 req202010) {
        String id = req202010.getId();
        Map<String,Object> detailMap=new HashMap<>();
    if (!StrUtil.isEmpty(id)) {
        MesCollection mesCollection = this.baseMapper.selectById(id);
        //短信内容
        String content = mesCollection.getContent();
        //发送结果
        String sendState = mesCollection.getSendState();
        //错误信息
        String errorMsg = mesCollection.getErrorMsg();
        List<MesDetail> mesDetails = mesDetailService.list(new QueryWrapper<MesDetail>().eq("relation_id", id));
        //手机条数
        int size = mesDetails.size();
        //手机号码
        String s="";
        for (MesDetail mesDetail : mesDetails) {
            if (s.length()>0){
                s+=",";
            }

            s+=mesDetail.getPhoneNumber();
        }


        //存储发送

        detailMap.put("content", content);
        detailMap.put("send_state", sendState);
        detailMap.put("error_msg", errorMsg);
        detailMap.put("pho_num", size);
        detailMap.put("phone_number", s);

        return detailMap;
    }
    return null;
    }
    /**
     * 短信信息-删
     * @param req202011
     * @return
     */
    public Object mesInfoDelbyIds(Req202011 req202011) {
        //删信息
        this.removeByIds(req202011.getIds());
        //删发送人
        LambdaQueryWrapper<MesDetail> qw = new LambdaQueryWrapper<>();
        qw.in(MesDetail::getRelationId, req202011.getIds());
        mesDetailService.remove(qw);
        return "Success";
    }
}
